cmake_minimum_required (VERSION 2.8) 
PROJECT(hadoopcppclient CXX C)
ENABLE_TESTING()
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)

if(UNIX)
set(CMAKE_CXX_FLAGS "--std=c++0x")
set(CMAKE_CXX_FLAGS_DEBUG "-D_GLIBCXX_DEBUG -ggdb3")
endif()

find_package (Threads)
find_package(OpenSSL REQUIRED)
find_package(Protobuf REQUIRED)
find_package(Qt4 4.8 REQUIRED QtCore QtNetwork)
include (CheckFunctionExists)
include (CheckIncludeFiles)
include (CheckIncludeFileCXX)
include (CheckCXXSourceCompiles)


check_include_file_cxx (inttypes.h HAVE_INTTYPES_H)
check_include_files (memory.h HAVE_MEMORY_H)
check_include_files (stdint.h HAVE_STDINT_H)
check_include_files (stdlib.h HAVE_STDLIB_H)

check_include_files (strings.h HAVE_STRINGS_H)
check_include_files (string.h HAVE_STRING_H)
check_include_files (sys/resource.h HAVE_SYS_RESOURCE_H)
check_include_files (sys/stat.h HAVE_SYS_STAT_H)
check_include_files (sys/time.h HAVE_SYS_TIME_H)
check_include_files (sys/types.h HAVE_SYS_TYPES_H)
check_include_files (sys/utsname.h HAVE_SYS_UTSNAME_H)

check_include_file_cxx (google/malloc_extension.h HAVE_GOOGLE_MALLOC_EXTENSION_H)


foreach(filename "unordered_map" "tr1/unordered_map")
  foreach(namespace  "std" "std::tr1")
    check_cxx_source_compiles(
          "
        #include <${filename}>
        int main(int argc,char* argv[]) {
            ${namespace}::unordered_map<int, int> m;
            return 0;
        }
    "
	      UNORDERED_MAP_FOUND_${filename}_${namespace})
	    
	    if (UNORDERED_MAP_FOUND_${filename}_${namespace})
	      set  (HAVE_HASH_MAP 1)
	         set  (HAVE_UNORDERED_MAP 1)
		    set (HASH_MAP_H "<${filename}>")
		       set (HASH_NAMESPACE "${namespace}")
		          break()
			  endif(UNORDERED_MAP_FOUND_${filename}_${namespace})    
			endforeach(namespace)
		      endforeach(filename)

foreach(filename "tr1/unordered_set" "unordered_set")
  foreach(namespace  "std" "std::tr1")
    check_cxx_source_compiles(
          "
        #include <${filename}>
        int main(int argc,char* argv[]) {
            ${namespace}::unordered_set<int, int> m;
            return 0;
        }
    "
	      UNORDERED_SET_FOUND_${filename}_${namespace})
	    
	    if (UNORDERED_SET_FOUND_${filename}_${namespace})   
	         set (HAVE_HASH_SET 1)
		    set (HASH_SET_H "<${filename}>")
		       break()
		       endif(UNORDERED_SET_FOUND_${filename}_${namespace})    
		     endforeach(namespace)
		   endforeach(filename)

foreach(filename "functional" "xfunctional" "tr1/functional" "ext/hash_fun.h" "ext/stl_hash_fun.h" "hash_fun.h" "stl_hash_fun.h" "stl/_hash_fun.h")
  check_cxx_source_compiles(
        "
        #include <${filename}>
        int main(int argc,char* argv[]) {
            int x = ${HASH_NAMESPACE}::hash<int>()(5);
            return 0;
        }
    "
	    HASH_FUNC_FOUND_${filename})
	  if (HASH_FUNC_FOUND_${filename})   
	       set (HASH_FUN_H "<${filename}>")
	          break()
		endif(HASH_FUNC_FOUND_${filename}) 
endforeach(filename)


include_directories(${PROJECT_SOURCE_DIR}/gtest/include)

check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
configure_file (
  "${PROJECT_SOURCE_DIR}/config.h.in"
  "${PROJECT_BINARY_DIR}/config.h"
 )
ADD_SUBDIRECTORY(gtest)  
ADD_SUBDIRECTORY(src bin)
ADD_SUBDIRECTORY(examples)

SET(CPACK_GENERATOR "DEB")
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Changming Sun")
include (CPack)
